<template>
  <a-layout>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <a-form
          layout="inline"
          :model="searchUser"
      >
        <a-form-item>
          <a-input v-model:value="searchUser.loginName" placeholder="按用户名查询">
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-space size="small">
            <a-button type="primary" @click="handleQueryByName({page:1, size: pagination.pageSize})">
              查询
            </a-button>
            <a-button type="primary" @click="add">
              新增
            </a-button>
          </a-space>
        </a-form-item>
      </a-form>
      <a-table :columns="columns"
               :row-key="record => record.id"
               :data-source="users"
               :pagination="pagination"
               :loading="loading"
               @change="handleTableChange">
        <template v-slot:action="{ text, record}">
          <a-space size="small">
            <a-button type="primary" @click="resetPassword(record)">
              重置密码
            </a-button>
            <a-button type="primary" @click="edit(record)">
              编辑
            </a-button>
            <a-popconfirm
                title="该删除操作不可逆-确定删除吗?"
                ok-text="确定"
                cancel-text="取消"
                @confirm="handleDelete(record.id)"
            >
              <a-button type="primary" danger>
                删除
              </a-button>
            </a-popconfirm>
          </a-space>
        </template>
      </a-table>
    </a-layout-content>
  </a-layout>
  <a-modal title="用户信息表单"
           v-model:visible="modalVisible"
           :confirm-loading="modalLoading"
           @ok="handleModalOk">
    <a-form :model="user" :label-col="{ span : 6}" :wrapper-col="wrapperCol">
      <a-form-item label="用户名">
        <a-input v-model:value="user.loginName" :disabled="!!user.id"/>
      </a-form-item>
      <a-form-item label="昵称">
        <a-input v-model:value="user.name"/>
      </a-form-item>
      <a-form-item label="密码" v-show="!user.id">
        <a-input v-model:value="user.password"/>
      </a-form-item>
    </a-form>
  </a-modal>

  <a-modal title="重置密码"
           v-model:visible="resetModalVisible"
           :confirm-loading="resetModalLoading"
           @ok="handleResetModalOk">
    <a-form :model="user" :label-col="{ span : 6}" :wrapper-col="wrapperCol">
      <a-form-item label="新密码">
        <a-input v-model:value="user.password"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts">
import {defineComponent, onMounted, ref} from 'vue';
import axios from "axios";
import {message} from 'ant-design-vue'
import {Tool} from "@/util/tool";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: 'AdminUser',
  setup() {
    // 用户信息响应式对象
    const users = ref();
    // 页码信息
    const pagination = ref({
      current: 1,
      pageSize: 10,
      total: 0
    });
    // 表示加载状态
    const loading = ref(false);
    // 表格列信息
    const columns = [
      {
        title: '用户名',
        dataIndex: 'loginName',
      },
      {
        title: '昵称',
        dataIndex: 'name',
      },
      {
        title: '密码',
        dataIndex: 'password',
      },
      {
        title: 'Action',
        key: 'action',
        slots: {customRender: 'action'},
      },
    ];
    // 用户信息查询对象
    const searchUser = ref();
    searchUser.value = {}
    /**
     * 数据查询
     */
    const handleQuery = (params: any) => {
      loading.value = true;
      users.value = [];
      axios.get("/user/list", {
        params: {
          page: params.page,
          size: params.size,
        }
      }).then((response) => {
        loading.value = false;
        const data = response.data;
        if (data.success) {
          users.value = data.content.list;
          // 重置分页按钮
          pagination.value.current = params.page;
          pagination.value.total = data.content.total;
        } else {
          message.error(data.message);
        }
      })
    };

    /**
     * 根据loginName查询用户信息
     */
    const handleQueryByName = (params: any) => {
      loading.value = true;
      users.value = [];
      axios.get("/user/search", {
        params: {
          page: params.page,
          size: params.size,
          loginName: searchUser.value.loginName
        }
      }).then((response) => {
        loading.value = false;
        const data = response.data;
        if (data.success) {
          users.value = data.content.list;
          // 重置分页按钮
          pagination.value.current = params.page;
          pagination.value.total = data.content.total;
        } else {
          message.error(data.message);
        }

      })
    };
    /**
     * 表格点击页面时激活操作
     */
    const handleTableChange = (pagination: any) => {
      handleQuery({
        page: pagination.current,
        size: pagination.pageSize
      });
    };
    // 表单
    const user = ref();
    const modalVisible = ref(false);
    const modalLoading = ref(false);
    const handleModalOk = () => {
      modalLoading.value = true;
      // 用户密码加盐MD5
      user.value.password = hexMd5(user.value.password + KEY);
      // 编辑保存用户信息
      axios.post("/user/save", user.value).then((response) => {
        modalLoading.value = true;
        loading.value = false;
        const data = response.data;
        if (data.success) {
          modalVisible.value = false;
          modalLoading.value = false;
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          })
          message.success("操作成功")
        } else {
          modalLoading.value = false;
          message.error(data.message)
        }
      })
    };

    // 编辑用户信息方法
    const edit = (record: any) => {
      modalVisible.value = true;
      user.value = Tool.copy(record);
    }

    // 新增用户信息方法
    const add = () => {
      modalVisible.value = true;
      user.value = {};
    }

    // 删除用户信息方法
    const handleDelete = (id: number) => {
      axios.delete("/user/delete/" + id).then((response) => {
        const data = response.data;
        if (data.success) {
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          })
          message.success("操作成功")
        }
      })
    }

    // 重置密码表单
    const resetModalVisible = ref(false);
    const resetModalLoading = ref(false);
    const handleResetModalOk = () => {
      resetModalLoading.value = true;
      // 用户密码加盐MD5
      user.value.password = hexMd5(user.value.password + KEY);
      // 编辑保存用户信息
      axios.post("/user/resetPassword", user.value).then((response) => {
        resetModalLoading.value = true;
        loading.value = false;
        const data = response.data;
        if (data.success) {
          resetModalVisible.value = false;
          resetModalLoading.value = false;
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          })
          message.success("操作成功")
        } else {
          resetModalLoading.value = false;
          message.error(data.message)
        }
      })
    };

    // 重置用户密码方法
    const resetPassword = (record: any) => {
      resetModalVisible.value = true;
      user.value = Tool.copy(record);
      user.value.password = '';
    }

    onMounted(() => {
      handleQuery({
        page: pagination.value.current,
        size: pagination.value.pageSize
      });
    });
    return {
      users,
      user,
      loading,
      columns,
      pagination,
      modalVisible,
      modalLoading,
      handleTableChange,
      edit,
      add,
      handleModalOk,
      handleDelete,
      searchUser,
      handleQueryByName,

      resetModalLoading,
      resetModalVisible,
      resetPassword,
      handleResetModalOk
    }
  }
});

</script>
